<!--头部公共部分-->
{include file="common/header" /}

<div style="padding:15px;font-size: 14px;">
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="font-size: 12px;">订单编号</label>
                <div class="layui-input-inline">
                    <input type="text" name="contract_number" readonly value='{$contract.contract_number}'
                           autocomplete="off"   class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="font-size: 12px;">学员姓名</label>
                <div class="layui-input-inline">
                    <input type="text" readonly name="student_name" value='{$contract.student_name}' autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" name="total_amount" style="font-size: 12px;">订单总额(元)</label>
                <div class="layui-input-inline">
                    <input type="text" readonly name="total_amount" value='{$contract.total_amount}' autocomplete="off"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: -10px;">
            <div class="layui-inline">
                <label class="layui-form-label" name="amount" style="font-size: 12px;"><span class="x-red"></span>已收金额(元)</label>
                <div class="layui-input-inline">
                    <input type="text" readonly name="amount" value='{$contract.amount}'
                           autocomplete="off"   class="layui-input">
                </div>
            </div>

            <script>

                let type = '{$type}'; //1:储值  0：常规
                if(type == 1){
                    document.write('<div class="layui-inline">');
                    document.write('<label class="layui-form-label" style="font-size: 12px;"><span class="x-red"></span>储值余额(元)</label>');
                    document.write('<div class="layui-input-inline">');
                    document.write('<input type="text"  name="balance" readonly value="{$contract.account_balance}" autocomplete="off"   class="layui-input">');
                    document.write('</div>');
                    document.write('</div>');
                }else if(type == 0){
                    document.write('<div class="layui-inline">');
                    document.write('<label class="layui-form-label" style="font-size: 12px;"><span class="x-red"></span>学费余额(元)</label>');
                    document.write('<div class="layui-input-inline">');
                    document.write('<input type="text"  name="balance" readonly value="{$contract.balance}" autocomplete="off"   class="layui-input">');
                    document.write('</div>');
                    document.write('</div>');
                }

            </script>

            <!--
            <div class="layui-inline">
                <label class="layui-form-label" style="font-size: 12px;"><span class="x-red"></span>账户余额(元)</label>
                <div class="layui-input-inline">
                    <input type="text"  name="balance" readonly value='{$contract.balance}' autocomplete="off"   class="layui-input">
                </div>
            </div>
            -->

            <div class="layui-inline">
                <label class="layui-form-label" style="font-size: 12px;"><span class="x-red">*</span>经手人</label>
                <div class="layui-input-inline">
                    <select name="experienced_person" lay-verify="required">
                        <option value="{$Think.session.user.id}">{$Think.session.user.name}</option>
                    
                    </select>
                </div>
            </div>
        </div>


                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="font-size: 12px;"><span class="x-red">*</span>退款通道</label>
                            <div class="layui-input-inline">
                                <select name="refund_channel" lay-verify="required">
                                    <option value="79">现金</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="font-size: 12px;"><span class="x-red">*</span>退款金额(元)</label>
                            <div class="layui-input-inline">
                                <input type="number" id="refund_amount" min="0" value="0" oninput="JudgeAmount()"  name="refund_amount" lay-verify="required" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="font-size: 12px;"><span class="x-red"></span>退款凭证号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="receipt_number" value='{$contract.receipt_number}' readonly  autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-sm layui-btn-primary"  style="width:80px;" id="cancle"  readonly>取消</button>
                        <input class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="form" style="width:80px;" value="保存" readonly>
                    </div>

    </form>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>

    function JudgeAmount() {
        var refund_amount = parseInt(document.getElementById("refund_amount").value);
        if(parseInt('{$type}') === 0){
            var balance = parseInt('{$contract.balance}');
            if((balance - refund_amount) < 0){
                $('input[name=refund_amount]').val(balance);
                layui.layer.msg('退款金额不能大于学费余额');
            }
        }else if(parseInt('{$type}') === 1){
            var storedvalue = parseInt('{$contract.storedvalue}');
            if((storedvalue - refund_amount) < 0){
                $('input[name=refund_amount]').val(balance);
                layui.layer.msg('退款金额不能大于账户余额');
            }
        }

    }

    layui.use(['element', 'jquery', 'form', 'laydate', 'layer'], function () {
        let $ = layui.$,
            layer = layui.layer,
            form = layui.form,
            element = layui.element,
            laydate = layui.laydate,
            jquery = layui.jquery;



        //监听提交
        form.on('submit(form)', function (data) {
            let type = parseInt('{$type}');
            var field = data.field;
            $.ajax({
                url: '/admin/contract/saverefund?type=' + type,
                method: "post",
                data: field,
                beforeSend: function () {

                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 1) {
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            parent.location.reload();
                        }, 1000);
                    }

                },
                complete: function () {
                },
                error: function (error) {
                    console.log(error)
                }
            });

            return false;
        });



        //取消
        $('#cancle').click(function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            return false;
        });
    });
</script>
